import request from "@/utils/request";

export interface Tablelist {
  id: string;
  orderPrice: string;
  status: string;
  remark: string;
  orderTime: string;
  customer: {
    id: string;
    nickName: string;
    phone: string;
    unit: string;
  };
  address: {
    id: string;
    name: string;
    phone: string;
    region: string;
    address: string;
  };
  orderItems: {
    id: string;
    pPrice: string;
    pNumber: string;
    product: {
      id: string;
      pName: string;
      pBrand: string;
      pType: string;
      pPrice: string;
      pImgUrl: string;
      isActive: boolean;
      pSpecifications: string;
      pIntroduce: string;
    };
  };
}
// 查询商品列表的参数类型
export interface ListQueryDto {
  keyword?: string; // 关键字
  page?: number; // 页数，默认1
  pagesize?: number; // 每页条数，默认10
}

export default {
  fileUpload(data: object) {
    return request({
      url: "/file/fileUpload",
      method: "post",
      data: data,
    });
  },
  addOrder(data: object) {
    return request({
      url: "/order/add",
      method: "post",
      data: data,
    });
  },
  editOrder(id: string, data: object) {
    return request({
      url: "/order/" + id,
      method: "patch",
      data: data,
    });
  },
  updateOrderStatus(id: string, data: object) {
    return request({
      url: "/order/" + id + "/status",
      method: "patch",
      data: data,
    });
  },
  deleteOrder(id: string) {
    return request({
      url: "/order/" + id,
      method: "delete",
    });
  },
  // 获取订单列表 - 建议使用 tableApi.packTableList 替代
  getTableList(params?: ListQueryDto) {
    return request({
      url: "/order/tableList",
      method: "get",
      params: params,
    });
  },
  // 批量删除订单
  batchDeleteSome(data: { orderIds: string[] }) {
    return request({
      url: "/order/batch",
      method: "delete",
      data: JSON.stringify(data),
      headers: {
        "Content-Type": "application/json",
      },
      transformRequest: [], // 禁用默认的URL编码转换
    });
  },

  // 发票相关API
  // 保存发票抬头信息
  saveInvoiceInfo(orderId: string, data: object) {
    return request({
      url: `/order/${orderId}/invoice/info`,
      method: "patch",
      data: data,
    });
  },

  // 上传发票图片
  uploadInvoiceImage(orderId: string, data: object) {
    return request({
      url: `/order/${orderId}/invoice/image`,
      method: "patch",
      data: data,
    });
  },

  // 获取发票信息
  getInvoiceInfo(orderId: string) {
    return request({
      url: `/order/${orderId}/invoice`,
      method: "get",
    });
  },

  // 下载订单Excel
  downloadOrdersExcel(params?: ListQueryDto & { orderIds?: string[] }) {
    // 处理数组参数，将orderIds转换为逗号分隔的字符串
    const processedParams = { ...params };
    if (processedParams.orderIds && Array.isArray(processedParams.orderIds)) {
      processedParams.orderIds = processedParams.orderIds.join(",");
    }

    return request({
      url: "/order/export/excel",
      method: "get",
      params: processedParams,
      responseType: "blob", // 重要：设置响应类型为blob以处理文件下载
    });
  },
};
